
(() => {

    let devId = sessionStorage.getItem('devId') // 设备标识
    let browserName = getBrowserName() // 浏览器名称

    console.log('browserName', browserName)

    if (!devId) {
        debugger
        devId = generateUUID()
        sessionStorage.setItem('devId', devId)
    }




















    const originalPushState = history.pushState;
    const originalReplaceState = history.replaceState;

    window.addEventListener('popstate', () => {
        debugger
    })


    history.pushState = function (state, title, url) {
        debugger;
        originalPushState.apply(this, arguments);
        // 触发自定义事件
        window.dispatchEvent(new CustomEvent('statechange', {
            detail: { type: 'pushstate', state, title, url }
        }));
    };

    history.replaceState = function (state, title, url) {
        debugger;
        originalReplaceState.apply(this, arguments);
        // 触发自定义事件
        window.dispatchEvent(new CustomEvent('statechange', {
            detail: { type: 'replacestate', state, title, url }
        }));
    };

    // 监听自定义事件
    window.addEventListener('statechange', function (e) {
        // addLog('history-log', e.detail.type, `URL: ${e.detail.url || location.href}`);
        debugger;
        console.log('history-log', e)
    });










    // 生成UUID
    function generateUUID() {
        // 1. 首先尝试使用crypto.randomUUID() (现代浏览器)
        if (typeof crypto !== 'undefined' && crypto.randomUUID) {
            return crypto.randomUUID() + '-' + Date.now()
        }

        // 2. 尝试使用crypto.getRandomValues() (IE11+ 和现代浏览器)
        if (typeof crypto !== 'undefined' && crypto.getRandomValues) {
            const buffer = new Uint8Array(16);
            crypto.getRandomValues(buffer);

            // 设置UUID版本和变体
            buffer[6] = (buffer[6] & 0x0f) | 0x40; // version 4
            buffer[8] = (buffer[8] & 0x3f) | 0x80; // variant

            // 转换为UUID字符串格式
            const hex = Array.from(buffer)
                .map(b => b.toString(16).padStart(2, '0'))
                .join('');

            return `${hex.substr(0, 8)}-${hex.substr(8, 4)}-${hex.substr(12, 4)}-${hex.substr(16, 4)}-${hex.substr(20)}` + '-' + Date.now();
        }

        // 3. 回退到基于Math.random()的实现 (最广泛的兼容性)
        return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
            const r = Math.random() * 16 | 0;
            const v = c === 'x' ? r : (r & 0x3 | 0x8);
            return v.toString(16) + '-' + Date.now();
        });
    }

    // 获取浏览器
    function getBrowserName() {
        const userAgent = navigator.userAgent.toLowerCase();
        
        // 基于Chromium的浏览器（需优先判断，避免被识别为Chrome）
        if (userAgent.includes("edg")) return "Edge";
        if (userAgent.includes("brave")) return "Brave";
        if (userAgent.includes("vivaldi")) return "Vivaldi";
        if (userAgent.includes("opr")) return "Opera";
        
        // 其他主流浏览器
        if (userAgent.includes("chrome") && !userAgent.includes("edge")) return "Chrome";
        if (userAgent.includes("firefox")) return "Firefox";
        if (userAgent.includes("safari") && !userAgent.includes("chrome")) return "Safari";
        if (userAgent.includes("trident")) return "Internet Explorer 11";
        if (userAgent.includes("msie")) return "Internet Explorer";
        
        // 国产浏览器
        if (userAgent.includes("samsungbrowser")) return "Samsung Internet";
        if (userAgent.includes("ucbrowser")) return "UC Browser";
        if (userAgent.includes("qqbrowser")) return "QQ Browser";
        if (userAgent.includes("360se")) return "360 安全浏览器";
        if (userAgent.includes("360ee")) return "360 极速浏览器";
        if (userAgent.includes("se 2.x")) return "搜狗浏览器";
        
        // 未知浏览器
        return "未知浏览器";
      }





})()